package com.ytjj.qmyx.supplychain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.kuaidi100.sdk.response.ProductCategoryResp;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.api.ResultCode;
import com.ytjj.qmyx.supplychain.common.constants.BankerConstants;
import com.ytjj.qmyx.supplychain.common.constants.RedisConstants;
import com.ytjj.qmyx.supplychain.common.exception.ApiException;
import com.ytjj.qmyx.supplychain.common.model.request.*;
import com.ytjj.qmyx.supplychain.common.model.request.YxAssignFreight;
import com.ytjj.qmyx.supplychain.common.model.response.*;
import com.ytjj.qmyx.supplychain.common.utils.OrderUtils;
import com.ytjj.qmyx.supplychain.mapper.*;
import com.ytjj.qmyx.supplychain.mapper.entity.*;
import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.beans.BeanMap;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BankerProductService extends AbstractService<BankerProductMapper, YxBankerProduct> {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private BankerProductMapper bankerProductMapper;
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private ProductAttributeValueMapper productAttributeValueMapper;
    @Resource
    private ProductAttributeKeyMapper productAttributeKeyMapper;
    @Resource
    private BankerProductSpecMapper bankerProductSpecMapper;
    @Resource
    private BankerSpecialMapper bankerSpecialMapper;
    @Resource
    private BankerProductSkuKeyMapper bankerProductSkuKeyMapper;
    @Resource
    private BankerProductSkuValueMapper bankerProductSkuValueMapper;
    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private BankerProductImgMapper bankerProductImgMapper;
    @Resource
    private BankerMessageMapper bankerMessageMapper;
    @Resource
    private BankerMapper bankerMapper;
    @Resource
    private FreightTemplateDao freightTemplateDao;
    @Resource
    private ProductSkuKeyMapper productSkuKeyMapper;
    @Resource
    private ProductBatchRuleMapper productBatchRuleMapper;
    @Resource
    private ProductBatchPriceMapper productBatchPriceMapper;
    @Resource
    private BankerSpecialService bankerSpecialService;
    @Autowired
    private BankerService bankerService;
    @Autowired
    private BankerProductSkuKeyService bankerProductSkuKeyService;
    @Autowired
    private BankerProductSkuValueService bankerProductSkuValueService;
    @Autowired
    private ProductCategoryService productCategoryService;
    @Autowired
    private BankerProductSpecService bankerProductSpecService;
    @Autowired
    private EvaluationProductService evaluationProductService;

    public CommonResult updateStatusByBrandId(Integer brandId, Integer status) {
        Example example = new Example(YxBankerProduct.class);
        example.createCriteria().andEqualTo("brandId", brandId);
        List<YxBankerProduct> products = selectByExample(example);
        products.stream().forEach(s -> {
            s.setStatus(status.byteValue());
        });
        updateBatchByPrimaryKeySelective(products);
        return CommonResult.success();
    }

    public CommonResult updateStatusByBankerId(Integer bankerId, Integer status) {
        Example example = new Example(YxBankerProduct.class);
        example.createCriteria().andEqualTo("bankerId", bankerId);
        List<YxBankerProduct> products = selectByExample(example);
        products.stream().forEach(s -> {
            s.setStatus(status.byteValue());
        });
        updateBatchByPrimaryKeySelective(products);
        return CommonResult.success();
    }

    public int selectByAddressIdAndBankerId(Integer bankerId, Integer addressId) {
        Example example = new Example(YxBankerProduct.class);
        example.createCriteria().andEqualTo("bankerId", bankerId).andEqualTo("addressId", addressId);
        int result = bankerProductMapper.selectCountByExample(example);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    public YxBankerProduct addBankerProduct(BankerProductRequest bankerProductRequest) {

        // 获取锁
        RLock lock = redissonClient.getLock(RedisConstants.BANKER_PRODUCT + bankerProductRequest.getProductName());
        try {
            boolean b = lock.tryLock(0, 5, TimeUnit.SECONDS);
            if (b) {

                // 属性value id 集合
                List<Integer> attributeValueIds = bankerProductRequest.getAttributeValueIds();
                if (attributeValueIds.size() <= 0) {
                    throw new ApiException("sku属性不能为空！");
                }
                // 根据选择的属性value id集合转化数据
                List<Map<String, Object>> list = this.getCheckedSpan(attributeValueIds);
                if (list.size() > 2) {
                    throw new ApiException("sku属性不能选择两个以上的key！");
                }
                String specJson = JSON.toJSONString(list);

                YxBankerProduct bankerProduct = saveBankerProduct(bankerProductRequest, specJson);


                // 新增商品规格表
                List<String> skuSpec = this.getDetailByJson(specJson);
                if (!CollectionUtils.isEmpty(skuSpec)) {
                    for (String skuSpecStr : skuSpec) {
                        YxBankerProductSpec bankerProductSpec = new YxBankerProductSpec();
                        bankerProductSpec.setProductId(bankerProduct.getId());
                        bankerProductSpec.setBankerId(bankerProduct.getBankerId());
                        bankerProductSpec.setSkuSpec(skuSpecStr);
                        bankerProductSpec.setPrice(bankerProductRequest.getCostPrice());
                        bankerProductSpec.setSalePrice(bankerProductRequest.getSalePrice());
                        bankerProductSpec.setProImg(bankerProduct.getProductImg());
                        bankerProductSpec.setStock(bankerProductRequest.getStock());
                        bankerProductSpec.setStatus((byte) 1);
                        bankerProductSpec.setCreateTime(new Date());
                        bankerProductSpecMapper.insertSelective(bankerProductSpec);
                    }
                }
                Map<Integer, YxBankerProductSkuKey> param = new HashMap<>();
                // 新增商品sku属性key-value表数据
                for (Integer attributeValueId : attributeValueIds) {

                    YxProductAttributeValue productAttributeValue = productAttributeValueMapper.selectByPrimaryKey(attributeValueId);
                    YxProductAttributeKey productAttributeKey = productAttributeKeyMapper.selectByPrimaryKey(productAttributeValue.getAttributeId());

                    YxBankerProductSkuKey yxBankerProductSkuKey = param.get(productAttributeKey.getId());
                    // 新增sku属性key
                    if (yxBankerProductSkuKey == null) {
                        YxBankerProductSkuKey bankerProductSkuKey = new YxBankerProductSkuKey();
                        bankerProductSkuKey.setPid(bankerProduct.getId());
                        bankerProductSkuKey.setKid(productAttributeKey.getId());
                        bankerProductSkuKey.setSort(productAttributeKey.getSort());
                        bankerProductSkuKey.setName(productAttributeKey.getAttributeName());
                        bankerProductSkuKey.setCreateTime(new Date());
                        bankerProductSkuKeyService.insertBankerProductSkuKey(bankerProductSkuKey);
                        param.put(productAttributeKey.getId(), bankerProductSkuKey);
                        yxBankerProductSkuKey = bankerProductSkuKey;
                    }

                    // 新增sku属性value
                    YxBankerProductSkuValue bankerProductSkuValue = new YxBankerProductSkuValue();
                    bankerProductSkuValue.setPid(bankerProduct.getId());
                    bankerProductSkuValue.setVid(attributeValueId);
                    bankerProductSkuValue.setKid(yxBankerProductSkuKey.getId());
                    bankerProductSkuValue.setValue(productAttributeValue.getAttributeValue());
                    bankerProductSkuValue.setSort(productAttributeValue.getSort());
                    bankerProductSkuValue.setStatus((byte) 1);
                    bankerProductSkuValue.setCreateTime(new Date());
                    bankerProductSkuValueService.insertBankerProductSkuValue(bankerProductSkuValue);
                }
                return bankerProduct;
            }
        } catch (Exception e) {
            lock.unlock();
            throw new ApiException(e);
        }
        throw new ApiException(ResultCode.EXE_FAILED);
    }

    private List<Map<String, Object>> getCheckedSpan(List<Integer> attributeValueIds) {
        Map<String, List<String>> checkedList = new HashMap<>();
        if (!CollectionUtils.isEmpty(attributeValueIds)) {
            for (int i = 0; i < attributeValueIds.size(); i++) {
                Integer attributeValueId = attributeValueIds.get(i);
                YxProductAttributeValue dataValue = productAttributeValueMapper.selectByPrimaryKey(attributeValueId);
                YxProductAttributeKey dataKey = productAttributeKeyMapper.selectByPrimaryKey(dataValue.getAttributeId());
                String key = dataKey.getAttributeName();
                List<String> listValue = checkedList.get(key);
                if (null == listValue || listValue.size() <= 0) {
                    List<String> list = new ArrayList<>();
                    list.add(dataValue.getAttributeValue());
                    checkedList.put(key, list);
                } else {
                    listValue.add(dataValue.getAttributeValue());
                    checkedList.put(key, listValue);
                }
            }
        }
        List<Map<String, Object>> list = new ArrayList<>();
        checkedList.forEach((key, value) -> {
            Map<String, Object> span = new LinkedHashMap<>();
            span.put("spanName", key);
            span.put("spanValue", value);
            list.add(span);
        });
        return list;
    }

    /**
     * 保存供应商商品
     *
     * @param bankerProductRequest
     */
    public YxBankerProduct saveBankerProduct(BankerProductRequest bankerProductRequest, String specJson) {
        YxBanker bankerInfo = bankerService.getUserInfo();
        YxBankerProduct bankerProduct = new YxBankerProduct();
        // 未审核
        bankerProduct.setAuditStatus(0);
        bankerProduct.setCreateTime(new Date());
        BeanUtils.copyProperties(bankerProductRequest, bankerProduct);
        bankerProduct.setProductNo(OrderUtils.getProductNo());
        bankerProduct.setBankerId(bankerInfo.getId());
        bankerProduct.setBankerName(bankerInfo.getName());
        bankerProduct.setWarehouseName(bankerInfo.getName());
        bankerProduct.setProductName(bankerProductRequest.getWarehouse());
        bankerProduct.setCreateTime(new Date());
        bankerProduct.setStatus((byte) 2);
        // 默认为：0元测评商品
        bankerProduct.setIsZeroProduct(1);
        // sku
        bankerProduct.setAttributeJson(specJson);
        bankerProductMapper.insertSelective(bankerProduct);

        YxBankerSpecial yxBankerSpecial = new YxBankerSpecial();
        yxBankerSpecial.setBankerColumnId(3);
        yxBankerSpecial.setBankerId(bankerInfo.getId());
        yxBankerSpecial.setBankerProductId(bankerProduct.getId());
        yxBankerSpecial.setStatus(1);
        yxBankerSpecial.setCreateTime(new Date());
        bankerSpecialMapper.insertSelective(yxBankerSpecial);
        return bankerProduct;
    }

    private List<String> getDetailByJson(String specJson) {
        JSONArray jsonArray = JSONArray.parseArray(specJson);
        List<String> spanlist = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONArray spanValues = jsonObject.getJSONArray("spanValue");
            for (int j = 0; j < spanValues.size(); j++) {
                String span = jsonObject.getString("spanName") + ":" + spanValues.getString(j);
                if (jsonArray.size() == 1) {
                    spanlist.add(span);
                    continue;
                }
                getSpan(i + 1, span, jsonArray, spanlist);
            }
            break;
        }
        return spanlist;
    }

    private void getSpan(int i, String span, JSONArray jsonArray, List spanlist) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        JSONArray spanValues = jsonObject.getJSONArray("spanValue");
        for (int j = 0; j < spanValues.size(); j++) {
            span = span + "," + jsonObject.getString("spanName") + ":" + spanValues.getString(j);
            if (i == jsonArray.size() - 1) {
                spanlist.add(span);
            } else {
                getSpan(i + 1, span, jsonArray, spanlist);
            }
            span = span.substring(0, span.indexOf("," + jsonObject.getString("spanName") + ":" + spanValues.getString(j)));
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeBankerProduct(Integer id) {
        log.info("===removeBankerProduct==={}", id);
        YxBanker userInfo = bankerService.getUserInfo();
        log.info("===removeBankerProduct==={}", userInfo.getId());
        YxBankerProduct bankerProduct = bankerProductMapper.selectByPrimaryKey(id);
        if (bankerProduct != null) {
            if (bankerProduct.getStatus() != 1) {
                // 删除供应商商品
                bankerProduct.setStatus((byte) 3);
                bankerProductMapper.updateByPrimaryKeySelective(bankerProduct);
                // 平台商品也删除
                Example example = new Example(YxProduct.class);
                example.createCriteria().andEqualTo("bankerProductId", id).andEqualTo("bankerId", userInfo.getId());
                example.setOrderByClause("id desc limit 1");
                YxProduct product = productMapper.selectOneByExample(example);
                if (product != null) {
                    product.setStatus((byte) 3);
                    productMapper.updateByPrimaryKey(product);
                }
                return;
            }
            throw new ApiException("该商品已经审核通过上架无法删除");
        }
    }

    public CommonPage<BankerProductResponse> getBankerProductList(BankerProductQueryRequest bankerProductQueryRequest) {
        YxBanker userInfo = bankerService.getUserInfo();
        if (userInfo.getLevel() != 0) {
            bankerProductQueryRequest.setBankerId(userInfo.getId());
        }
        PageHelper.startPage(bankerProductQueryRequest.getPageNum(), bankerProductQueryRequest.getPageSize());
        List<BankerProductResponse> bankerProductList = bankerProductMapper.getBankerProductList(bankerProductQueryRequest);
        if (CollectionUtils.isEmpty(bankerProductList)){
            return CommonPage.restPage(new ArrayList<>());
        }
        List<Integer> bankerProductIds = bankerProductList.stream().map(BankerProductResponse::getId).collect(Collectors.toList());

        Example example = new Example(YxBankerProductSpec.class);
        example.createCriteria().andIn("productId", bankerProductIds);
        List<YxBankerProductSpec> yxBankerProductSpecList = bankerProductSpecMapper.selectByExample(example);


        List<YxProductCategory> yxProductCategories = productCategoryService.selectAll();
        if (!CollectionUtils.isEmpty(bankerProductList)) {
            for (BankerProductResponse bp : bankerProductList) {
                List<YxBankerProductSpec> collect = yxBankerProductSpecList.stream().filter(item -> item.getProductId().equals(bp.getId())).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(collect)){
                    final Integer[] stockSum = {0};
                    collect.stream().forEach(item ->{
                        stockSum[0] = stockSum[0] + item.getStock();

                    });
                    List<BigDecimal> costPriceList = collect.stream().filter(item -> null != item.getPrice()).map(YxBankerProductSpec::getPrice).collect(Collectors.toList());
                    bp.setStock(stockSum[0]);
                    if (!CollectionUtils.isEmpty(costPriceList)){
                        BigDecimal min = Collections.min(costPriceList);
                        bp.setCostPrice(min);
                    }
                }
                // 获取一、二、三级分类标题
                for (YxProductCategory yxProductCategory : yxProductCategories) {
                    if (!StringUtils.isEmpty(bp.getTopCategoryId()) && bp.getTopCategoryId().equals(yxProductCategory.getId())) {
                        bp.setTopCategoryName(yxProductCategory.getTitle());
                    }
                    if (!StringUtils.isEmpty(bp.getCategoryId()) && bp.getCategoryId().equals(yxProductCategory.getId())) {
                        bp.setChildCategoryName(yxProductCategory.getTitle());
                    }
                    if (!StringUtils.isEmpty(bp.getBottomCategoryId()) && bp.getBottomCategoryId().equals(yxProductCategory.getId())) {
                        bp.setBottomCategoryName(yxProductCategory.getTitle());
                    }
                }
            }
        }
        return CommonPage.restPage(bankerProductList);
    }

    /**
     * 不限供应商
     * @param bankerProductQueryRequest
     * @return
     */
    public CommonPage<BankerProductResponse> getAllBankerProductList(BankerProductQueryRequest bankerProductQueryRequest) {
        PageHelper.startPage(bankerProductQueryRequest.getPageNum(), bankerProductQueryRequest.getPageSize());
        List<BankerProductResponse> bankerProductList = bankerProductMapper.getBankerProductList(bankerProductQueryRequest);

        List<YxProductCategory> yxProductCategories = productCategoryService.selectAll();
        if (!CollectionUtils.isEmpty(bankerProductList)) {
            for (BankerProductResponse bp : bankerProductList) {
                // 获取一、二、三级分类标题
                for (YxProductCategory yxProductCategory : yxProductCategories) {
                    if (!StringUtils.isEmpty(bp.getTopCategoryId()) && bp.getTopCategoryId().equals(yxProductCategory.getId())) {
                        bp.setTopCategoryName(yxProductCategory.getTitle());
                    }
                    if (!StringUtils.isEmpty(bp.getCategoryId()) && bp.getCategoryId().equals(yxProductCategory.getId())) {
                        bp.setChildCategoryName(yxProductCategory.getTitle());
                    }
                    if (!StringUtils.isEmpty(bp.getBottomCategoryId()) && bp.getBottomCategoryId().equals(yxProductCategory.getId())) {
                        bp.setBottomCategoryName(yxProductCategory.getTitle());
                    }
                }
            }
        }
        return CommonPage.restPage(bankerProductList);
    }

    public List<BankerProductResponse> exportDataToExcel(BankerProductQueryRequest bankerProductQueryRequest) {
        YxBanker userInfo = bankerService.getUserInfo();
        if (userInfo.getLevel() != 0) {
            bankerProductQueryRequest.setBankerId(userInfo.getId());
        }
        List<BankerProductResponse> bankerProductResponses = bankerProductMapper.exportDataToExcel(bankerProductQueryRequest);
        List<YxProductCategory> yxProductCategories = productCategoryService.selectAll();
        if (!CollectionUtils.isEmpty(bankerProductResponses)) {
            for (BankerProductResponse bp : bankerProductResponses) {
                // 获取一、二、三级分类标题
                for (YxProductCategory yxProductCategory : yxProductCategories) {
                    if (!StringUtils.isEmpty(bp.getCategoryId()) && bp.getCategoryId().equals(yxProductCategory.getId())) {
                        bp.setChildCategoryName(yxProductCategory.getTitle());
                    }
                    if (!StringUtils.isEmpty(bp.getBottomCategoryId()) && bp.getBottomCategoryId().equals(yxProductCategory.getId())) {
                        bp.setBottomCategoryName(yxProductCategory.getTitle());
                    }
                }
            }
        }
        return bankerProductResponses;
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateBankerProductInfo(BankerProductRequest bankerProductRequest) {
        YxBanker userInfo = bankerService.getUserInfo();
        // 更新前

        // 属性value id 集合
        List<Integer> attributeValueIds = bankerProductRequest.getAttributeValueIds();
        if (attributeValueIds.size() <= 0) {
            throw new ApiException("sku属性不能为空！");
        }

        // 判断sku属性的key值是否跟新增一致
        List<Map<String, Object>> list = this.getCheckedSpan(attributeValueIds);
        // 查询商品选择的属性 key 集合
        List<YxBankerProductSkuKey> bankerProductSkuKeyList = bankerProductSkuKeyService.getBankerProductSkuKeysByPid(bankerProductRequest.getId());


        if (bankerProductSkuKeyList.size() != list.size()) {
            throw new ApiException("添加商品的sku属性key跟编辑不一致！");
        }

        String specJson = JSON.toJSONString(list);
        // 改变商品规格的 sku属性值
        YxBankerProduct bankerProduct = new YxBankerProduct();
        bankerProduct.setId(bankerProductRequest.getId());
        bankerProduct.setAttributeJson(specJson);
        bankerProductMapper.updateByPrimaryKeySelective(bankerProduct);

        // 查询商品原来添加的属性
        List<YxBankerProductSkuValue> bankerProductSkuValues = bankerProductSkuValueService.getBankerProductSkuValue(bankerProductRequest.getId());
        List<Integer> afterAttributeValueIds = bankerProductSkuValues.stream().map(YxBankerProductSkuValue::getVid).collect(Collectors.toList());

        // 新增/停用 商品属性规格表

        // 先停用所有商品属性规格表
        List<YxBankerProductSpec> bankerProductSpecList = bankerProductSpecService.getBankerProductSpecListByBankerIdAndProductId(userInfo.getId(), bankerProductRequest.getId());
        if (!CollectionUtils.isEmpty(bankerProductSpecList)) {
            for (YxBankerProductSpec yxBankerProductSpec : bankerProductSpecList) {
                yxBankerProductSpec.setStatus((byte) 0);
                bankerProductSpecMapper.updateByPrimaryKeySelective(yxBankerProductSpec);
            }
        }

        List<String> skuSpecList = this.getDetailByJson(specJson);
        if (!CollectionUtils.isEmpty(skuSpecList)) {
            for (String skuSpec : skuSpecList) {
                BankerProductSpecRequest bankerProductSpecRequest = new BankerProductSpecRequest();
                bankerProductSpecRequest.setSkuSpec(skuSpec);
                bankerProductSpecRequest.setBankerId(userInfo.getId());
                bankerProductSpecRequest.setProductId(bankerProductRequest.getId());
                List<YxBankerProductSpec> bankerProductSpecs = bankerProductSpecService.getBankerProductSpecList(bankerProductSpecRequest);
                if (!CollectionUtils.isEmpty(bankerProductSpecs)) {
                    YxBankerProductSpec yxBankerProductSpec = bankerProductSpecs.get(0);
                    yxBankerProductSpec.setStatus((byte) 1);
                    bankerProductSpecMapper.updateByPrimaryKeySelective(yxBankerProductSpec);
                } else {
                    YxBankerProductSpec bankerProductSpec = new YxBankerProductSpec();
                    bankerProductSpec.setProductId(bankerProductRequest.getId());
                    bankerProductSpec.setSkuSpec(skuSpec);
                    bankerProductSpec.setBankerId(userInfo.getId());
                    bankerProductSpec.setStock(0);
                    bankerProductSpec.setStatus((byte) 1);
                    bankerProductSpec.setCreateTime(new Date());
                    bankerProductSpecMapper.insertSelective(bankerProductSpec);
                }
            }
        }
        // 清空商品sku属性key-value表数据
        List<YxBankerProductSkuKey> bankerProductSkuKeys = bankerProductSkuKeyService.getBankerProductSkuKeysByPid(bankerProductRequest.getId());
        if (!CollectionUtils.isEmpty(bankerProductSkuKeys)) {
            for (YxBankerProductSkuKey productSkuKey : bankerProductSkuKeys) {
                bankerProductSkuKeyService.deleteByPrimaryKey(productSkuKey.getId());
            }
        }
        List<YxBankerProductSkuValue> bankerProductSkuValue = bankerProductSkuValueService.getBankerProductSkuValue(bankerProductRequest.getId());
        if (!CollectionUtils.isEmpty(bankerProductSkuValues)) {
            for (YxBankerProductSkuValue productSkuValue : bankerProductSkuValues) {
                bankerProductSkuValueService.deleteByPrimaryKey(productSkuValue.getId());
            }
        }


        // 更新商品sku属性key-value表数据
        Map<Integer, Object> param = new HashMap<>();

        for (Integer attributeValueId : attributeValueIds) {
            YxProductAttributeValue productAttributeValue = productAttributeValueMapper.selectByPrimaryKey(attributeValueId);
            YxProductAttributeKey productAttributeKey = productAttributeKeyMapper.selectByPrimaryKey(productAttributeValue.getAttributeId());

            // 新增sku属性key
            if (param.get(productAttributeKey.getId()) == null) {
                YxBankerProductSkuKey productSkuKey = new YxBankerProductSkuKey();
                productSkuKey.setPid(bankerProductRequest.getId());
                productSkuKey.setKid(productAttributeKey.getId());
                productSkuKey.setSort(productAttributeKey.getSort());
                productSkuKey.setName(productAttributeKey.getAttributeName());
                productSkuKey.setCreateTime(new Date());
                bankerProductSkuKeyMapper.insert(productSkuKey);
                param.put(productAttributeKey.getId(), productSkuKey);
            }

            // 新增sku属性value
            YxBankerProductSkuValue productSkuValue = new YxBankerProductSkuValue();
            productSkuValue.setPid(bankerProductRequest.getId());
            productSkuValue.setVid(attributeValueId);
            productSkuValue.setValue(productAttributeValue.getAttributeValue());
            productSkuValue.setSort(productAttributeValue.getSort());
            productSkuValue.setStatus((byte) 1);
            productSkuValue.setCreateTime(new Date());
            bankerProductSkuValueMapper.insert(productSkuValue);
        }

        bankerProductRequest.setUpdateTime(new Date());
        YxBankerProduct yxBankerProduct = new YxBankerProduct();
        BeanUtils.copyProperties(bankerProductRequest, yxBankerProduct);
        updateByPrimaryKeySelective(yxBankerProduct);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateBankerProduct(BankerProductRequest bankerProductRequest) {
        // 其他信息修改
        YxBanker userInfo = bankerService.getUserInfo();
        YxBankerSpecial bankerSpecial = bankerSpecialService.getBankerSpecial(userInfo.getId(), bankerProductRequest.getId());
        // 传入的参数为非0元测评商品 (需求已改)
        // 需求改为：供应商新增/编辑 产品类型均为：0元测评商品
        // (逻辑代码暂时保留，前端默认传值为 bankerColumnId=3(0元测评))
        if (BankerConstants.ZERO_ASSESSMENT.equals(bankerProductRequest.getBankerColumnId())) {
            bankerProductRequest.setUpdateTime(new Date());
            bankerProductRequest.setIsZeroProduct(1);
            bankerProductRequest.setPrice(new BigDecimal(0));
        } else {
            bankerProductRequest.setUpdateTime(new Date());
            bankerProductRequest.setIsZeroProduct(0);
        }
        //执行编辑后 审核状态都重置为：未审核状态
        bankerProductRequest.setAuditStatus(0);
        YxBankerProduct bankerProduct = new YxBankerProduct();
        BeanUtils.copyProperties(bankerProductRequest, bankerProduct);
        bankerProductMapper.updateByPrimaryKeySelective(bankerProduct);
        if (bankerSpecial == null) {
            bankerSpecial = new YxBankerSpecial();
            bankerSpecial.setBankerColumnId(bankerProductRequest.getBankerColumnId());
            bankerSpecial.setBankerId(userInfo.getId());
            bankerSpecial.setCreateTime(new Date());
            bankerSpecial.setBankerProductId(bankerProductRequest.getId());
            bankerSpecialMapper.insertSelective(bankerSpecial);
        } else {
            bankerSpecial.setBankerColumnId(bankerProductRequest.getBankerColumnId());
            bankerSpecial.setUpdateTime(new Date());
            bankerSpecialMapper.updateByPrimaryKeySelective(bankerSpecial);
        }
    }

    public Integer updateProductStatus(Integer id) {
        YxBankerProduct bankerProduct = bankerProductMapper.selectByPrimaryKey(id);
        Integer count = 0;
        if (bankerProduct.getStatus() == 1) {
            Integer status = evaluationProductService.queryStatusByBankerProductId(id);
            if (1 == status) {
                throw new ApiException("该商品已进入优质产品库，状态为上架，暂无法进行下架操作！");
            } else {
                bankerProduct.setStatus(new Byte("2"));
            }
        } else {
            bankerProduct.setStatus(new Byte("1"));
        }
        count = bankerProductMapper.updateByPrimaryKey(bankerProduct);
        return count;
    }

    /**
     * 修改商品信息，无需审核
     */
    public Integer updateProductNoCheck(BankerProductRequest bankerProductRequest){
        // 设置需要比较的日期字符串
        String dateString = "2024-03-20";
        // 将字符串转换为 LocalDate 对象
        LocalDate targetDate = LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE);
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 比较日期
        if (currentDate.isAfter(targetDate)) {
            throw new ApiException("该操作仅限于"+dateString+"前修改！如需编辑，请提交审核！");
        }
        String qualityReport = bankerProductRequest.getQualityReport();
        if(StringUtils.isEmpty(qualityReport)){
            throw new ApiException("请上传商品质检报告！");
        }
        YxBankerProduct product = new YxBankerProduct();
        product.setQualityReport(qualityReport);
        product.setId(bankerProductRequest.getId());
        int i = bankerProductMapper.updateByPrimaryKeySelective(product);
        return i;
    }

    public CommonPage<BankerProductResponse> selectList(BankerProductRequest bankerProductRequest) {
        PageHelper.startPage(bankerProductRequest.getPageNum(), bankerProductRequest.getPageSize());
        List<BankerProductResponse> pageList = bankerProductMapper.selectList(bankerProductRequest);
        if(!CollectionUtils.isEmpty(pageList)) {
            Set<Integer> categoryIds = pageList.stream().filter(bp -> null != bp.getTopCategoryId()).map(BankerProductResponse::getTopCategoryId).collect(Collectors.toSet());
            categoryIds.addAll(pageList.stream().filter(bp -> null != bp.getCategoryId()).map(BankerProductResponse::getCategoryId).collect(Collectors.toSet()));
            categoryIds.addAll(pageList.stream().filter(bp -> null != bp.getBottomCategoryId()).map(BankerProductResponse::getBottomCategoryId).collect(Collectors.toSet()));
            List<Integer> ids = new ArrayList<>(categoryIds);
            List<ProductCategoryResp> productCategoryRespList = productCategoryMapper.selectCategoryByIds(ids);
            for(BankerProductResponse bp : pageList) {
//                // 供应商商品->商品管理 所属活动
//                List<String> activityList = productDao.getActivityType(bp.getId());
//                if (!CollectionUtils.isEmpty(activityList)) {
//                    String activityType = activityList.get(0);
//                    bp.setActivityType(ProductActivityTypeEnums.getName(activityType));
//                }

                if(bp.getTopCategoryId() != null) {
                    List<ProductCategoryResp> topCategoryList = productCategoryRespList.stream().filter(category -> bp.getTopCategoryId().equals(category.getId())).collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(topCategoryList)){
                        bp.setOneTitle(topCategoryList.get(0).getTitle());
                    }
                }
                if(bp.getCategoryId() != null) {
                    List<ProductCategoryResp> CategoryList = productCategoryRespList.stream().filter(category -> bp.getTopCategoryId().equals(category.getId())).collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(CategoryList)){
                        bp.setTwoTitle(CategoryList.get(0).getTitle());
                    }
                }
                if(bp.getBottomCategoryId() != null) {
                    List<ProductCategoryResp> bottomCategoryList = productCategoryRespList.stream().filter(category -> bp.getTopCategoryId().equals(category.getId())).collect(Collectors.toList());
                    if (!CollectionUtils.isEmpty(bottomCategoryList)){
                        bp.setThreeTitle(bottomCategoryList.get(0).getTitle());
                    }
                }
            }
        }
        return CommonPage.restPage(pageList);
    }

    public BankerProductDetailResponse productDetail(Integer productId) {
        BankerProductDetailResponse detailInfo = new BankerProductDetailResponse();
        List<BankerProductDetailResponse> bankerProductDetailResponseList = bankerProductMapper.findSupplyProductByIdList(productId);


        if (!CollectionUtils.isEmpty(bankerProductDetailResponseList)) {
            if (bankerProductDetailResponseList.size() == 1) {
                detailInfo = bankerProductDetailResponseList.get(0);
                FreightTemplateResponse freightTemplateResponse = productMapper.findFreight(bankerProductDetailResponseList.get(0).getFreightId());
                if (null != freightTemplateResponse && null != freightTemplateResponse.getId()){
                    List<YxAssignFreight> yxAssignFreightList = freightTemplateDao.queryYxAssignFreightByFreightId(freightTemplateResponse.getId());
                    List<YxAssignFreightResponse> yxAssignFreightResponses = new ArrayList<>();
                    yxAssignFreightList.stream().forEach(item ->{
                        YxAssignFreightResponse yxAssignFreightResponse = new YxAssignFreightResponse();
                        BeanUtils.copyProperties(item, yxAssignFreightResponse);
                        yxAssignFreightResponses.add(yxAssignFreightResponse);

                    });
                    freightTemplateResponse.setAssignFreightList(yxAssignFreightResponses);
                }
                detailInfo.setFreightTemplateResponse(freightTemplateResponse);
            }else {
                bankerProductDetailResponseList.stream().sorted((stu1, stu2) ->
                        Long.compare(stu2.getProductCreatTime().getTime(), stu1.getProductCreatTime().getTime()))
                        .forEach(System.out::println);
                detailInfo = bankerProductDetailResponseList.get(0);
                if (detailInfo.getBrandId().equals(0)){
                    detailInfo.setBankerId(null);
                }
                FreightTemplateResponse freightTemplateResponse = productMapper.findFreight(bankerProductDetailResponseList.get(0).getFreightId());
                if (null != freightTemplateResponse && null != freightTemplateResponse.getId()){
                    List<YxAssignFreight> yxAssignFreightList = freightTemplateDao.queryYxAssignFreightByFreightId(freightTemplateResponse.getId());
                    List<YxAssignFreightResponse> yxAssignFreightResponses = new ArrayList<>();
                    yxAssignFreightList.stream().forEach(item ->{
                        YxAssignFreightResponse yxAssignFreightResponse = new YxAssignFreightResponse();
                        BeanUtils.copyProperties(item, yxAssignFreightResponse);
                        yxAssignFreightResponses.add(yxAssignFreightResponse);

                    });
                    freightTemplateResponse.setAssignFreightList(yxAssignFreightResponses);
                }
                detailInfo.setFreightTemplateResponse(freightTemplateResponse);
            }
        }

        List<ProductSkuKeyRequest> skuKey = productSkuKeyMapper.findSku(detailInfo.getId());
        List<BankerProductImgResponse> bannerList = productSkuKeyMapper.findImg(detailInfo.getId(), "banner");
        List<BankerProductImgResponse> infoList = productSkuKeyMapper.findImg(detailInfo.getId(),"info");
        List<BankerProductSpecResponse> spec  = productSkuKeyMapper.findSpec(detailInfo.getId());

        detailInfo.setSkuKey(skuKey);
        detailInfo.setBannerList(bannerList);
        detailInfo.setInfoList(infoList);
        detailInfo.setSpec(spec);

        YxBanker yxBanker = bankerMapper.selectByPrimaryKey(detailInfo.getBankerId());
        detailInfo.setBankerName(yxBanker.getName());

        List<Map<String, Object>> specMapList = queryProductSpecBatchPriceBySupply(spec, detailInfo.getBankerProductId());
        detailInfo.setSpecArr(specMapList);

        Example batchNumExample = new Example(YxProductBatchRule.class);
        batchNumExample.createCriteria().andEqualTo("productId", detailInfo.getBankerProductId()).andEqualTo("status", 1);
        List<YxProductBatchRule> batchRuleList = productBatchRuleMapper.selectByExample(batchNumExample);
        List<String> wholesaleNameArr = new ArrayList<>();
        if (!CollectionUtils.isEmpty(batchRuleList)) {
            batchRuleList.stream().forEach(item -> {
                String ruleKey = item.getMin() + "-" + item.getMax();
                wholesaleNameArr.add(ruleKey);
            });
        }
        detailInfo.setWholesaleNameArr(wholesaleNameArr);
        return detailInfo;
    }



    public BankerProductDetailResponse bankerProductDetail(Integer productId) {
        BankerProductDetailResponse detailInfo = new BankerProductDetailResponse();
        List<BankerProductDetailResponse> bankerProductDetailResponseList = bankerProductMapper.findInfoByBankerProductIdList(productId);



        FreightTemplateResponse freightTemplateResponse;
        if (!CollectionUtils.isEmpty(bankerProductDetailResponseList)) {
            if (bankerProductDetailResponseList.size() == 1) {
                detailInfo = bankerProductDetailResponseList.get(0);
                freightTemplateResponse = productMapper.findFreight(bankerProductDetailResponseList.get(0).getFreightId());
                if (null != freightTemplateResponse && null != freightTemplateResponse.getId()){
                    List<YxAssignFreight> yxAssignFreightList = freightTemplateDao.queryYxAssignFreightByFreightId(freightTemplateResponse.getId());
                    List<YxAssignFreightResponse> yxAssignFreightResponses = new ArrayList<>();
                    yxAssignFreightList.stream().forEach(item ->{
                        YxAssignFreightResponse yxAssignFreightResponse = new YxAssignFreightResponse();
                        BeanUtils.copyProperties(item, yxAssignFreightResponse);
                        yxAssignFreightResponses.add(yxAssignFreightResponse);

                    });
                    freightTemplateResponse.setAssignFreightList(yxAssignFreightResponses);
                }
                detailInfo.setFreightTemplateResponse(freightTemplateResponse);
            }else {
                bankerProductDetailResponseList.stream().sorted((stu1, stu2) ->
                        Long.compare(stu2.getProductCreatTime().getTime(), stu1.getProductCreatTime().getTime()))
                        .forEach(System.out::println);
                detailInfo = bankerProductDetailResponseList.get(0);
                freightTemplateResponse= productMapper.findFreight(bankerProductDetailResponseList.get(0).getFreightId());
                if (null != freightTemplateResponse && null != freightTemplateResponse.getId()){
                    List<YxAssignFreight> yxAssignFreightList = freightTemplateDao.queryYxAssignFreightByFreightId(freightTemplateResponse.getId());
                    List<YxAssignFreightResponse> yxAssignFreightResponses = new ArrayList<>();
                    yxAssignFreightList.stream().forEach(item ->{
                        YxAssignFreightResponse yxAssignFreightResponse = new YxAssignFreightResponse();
                        BeanUtils.copyProperties(item, yxAssignFreightResponse);
                        yxAssignFreightResponses.add(yxAssignFreightResponse);

                    });
                    freightTemplateResponse.setAssignFreightList(yxAssignFreightResponses);
                }
                detailInfo.setFreightTemplateResponse(freightTemplateResponse);
            }
        }

        List<ProductSkuKeyRequest> skuKey = bankerProductSkuKeyMapper.findSku(detailInfo.getId());
        List<BankerProductImgResponse> bannerList = bankerProductSkuKeyMapper.findImg(detailInfo.getId(), "banner");
        List<BankerProductImgResponse> infoList = bankerProductSkuKeyMapper.findImg(detailInfo.getId(),"info");
        List<BankerProductSpecResponse> spec  = bankerProductSkuKeyMapper.findSpec(detailInfo.getId());
        detailInfo.setSkuKey(skuKey);
        detailInfo.setBannerList(bannerList);
        detailInfo.setInfoList(infoList);
        detailInfo.setSpec(spec);
        List<Map<String, Object>> specMapList = queryProductSpecBatchPrice(spec, productId);
        detailInfo.setSpecArr(specMapList);

        Example batchNumExample = new Example(YxProductBatchRule.class);
        batchNumExample.createCriteria().andEqualTo("productId", productId).andEqualTo("status", 1);
        List<YxProductBatchRule> batchRuleList = productBatchRuleMapper.selectByExample(batchNumExample);
        List<String> wholesaleNameArr = new ArrayList<>();
        if (!CollectionUtils.isEmpty(batchRuleList)) {
            batchRuleList.stream().forEach(item -> {
                String ruleKey = item.getMin() + "-" + item.getMax();
                wholesaleNameArr.add(ruleKey);
            });
        }
        detailInfo.setWholesaleNameArr(wholesaleNameArr);
        return detailInfo;
    }

    /**
     * 规格的批发价(供应链来查)
     * @param specList
     * @param productId
     * @return
     */
    private List<Map<String, Object>> queryProductSpecBatchPriceBySupply(List<BankerProductSpecResponse> specList, Integer productId) {
        List<Map<String, Object>> spec = new ArrayList<>();
        specList.stream().forEach(item -> {
            Map<String, Object> specMap = new HashMap();
            BeanMap beanMap = BeanMap.create(item);
            for (Object key : beanMap.keySet()) {
                specMap.put(key + "", beanMap.get(key));
            }
            Example batchPriceExample = new Example(YxProductBatchPrice.class);
            batchPriceExample.createCriteria().andEqualTo("productId", productId)
                    .andEqualTo("specId", item.getSupplyId())
                    .andEqualTo("status", 1);
            List<YxProductBatchPrice> batchPriceList = productBatchPriceMapper.selectByExample(batchPriceExample);
            List<Integer> batchRuleIds = null;
            if (!CollectionUtils.isEmpty(batchPriceList)) {
                batchRuleIds = batchPriceList.stream().map(YxProductBatchPrice::getBatchRuleId).collect(Collectors.toList());
            }
            if (!CollectionUtils.isEmpty(batchRuleIds)) {
                Example batchRuleExample = new Example(YxProductBatchRule.class);
                batchRuleExample.createCriteria().andIn("id", batchRuleIds);
                List<YxProductBatchRule> batchRuleList = productBatchRuleMapper.selectByExample(batchRuleExample);
                if (!CollectionUtils.isEmpty(batchRuleList)) {
                    batchRuleList.stream().forEach(rule -> {
                        String ruleKey = rule.getMin() + "-" + rule.getMax();
                        List<YxProductBatchPrice> matchPriceList = batchPriceList.stream().filter(batchPrice -> batchPrice.getBatchRuleId().equals(rule.getId())).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(matchPriceList)) {
                            specMap.put(ruleKey, matchPriceList.get(0).getPrice());
                        }
                    });
                }
            }
            spec.add(specMap);
        });
        return spec;
    }

    /**
     * 规格的批发价
     * @param specList
     * @param productId
     * @return
     */
    private List<Map<String, Object>> queryProductSpecBatchPrice(List<BankerProductSpecResponse> specList, Integer productId) {
        List<Map<String, Object>> spec = new ArrayList<>();
        specList.stream().forEach(item -> {
            Map<String, Object> specMap = new HashMap();
            BeanMap beanMap = BeanMap.create(item);
            for (Object key : beanMap.keySet()) {
                specMap.put(key + "", beanMap.get(key));
            }
            Example batchPriceExample = new Example(YxProductBatchPrice.class);
            batchPriceExample.createCriteria().andEqualTo("productId", productId)
                    .andEqualTo("specId", item.getId())
                    .andEqualTo("status", 1);
            List<YxProductBatchPrice> batchPriceList = productBatchPriceMapper.selectByExample(batchPriceExample);
            List<Integer> batchRuleIds = null;
            if (!CollectionUtils.isEmpty(batchPriceList)) {
                batchRuleIds = batchPriceList.stream().map(YxProductBatchPrice::getBatchRuleId).collect(Collectors.toList());
            }
            if (!CollectionUtils.isEmpty(batchRuleIds)) {
                Example batchRuleExample = new Example(YxProductBatchRule.class);
                batchRuleExample.createCriteria().andIn("id", batchRuleIds);
                List<YxProductBatchRule> batchRuleList = productBatchRuleMapper.selectByExample(batchRuleExample);
                if (!CollectionUtils.isEmpty(batchRuleList)) {
                    batchRuleList.stream().forEach(rule -> {
                        String ruleKey = rule.getMin() + "-" + rule.getMax();
                        List<YxProductBatchPrice> matchPriceList = batchPriceList.stream().filter(batchPrice -> batchPrice.getBatchRuleId().equals(rule.getId())).collect(Collectors.toList());
                        if (!CollectionUtils.isEmpty(matchPriceList)) {
                            specMap.put(ruleKey, matchPriceList.get(0).getPrice());
                        }
                    });
                }
            }
            spec.add(specMap);
        });
        return spec;
    }

    @Transactional(rollbackFor = Exception.class)
    public Integer syncBankerToProduct(ProductCheckRequest product) {
        Example example = new Example(YxProduct.class);
        example.createCriteria().andEqualTo("bankerProductId", product.getId())
                .andNotIn("status", Arrays.asList(2, 3, 5));
        Integer isExists = productMapper.selectCountByExample(example);
        if(isExists > 0) {
            throw new ApiException("该供应商商品已审核通过，暂不支持重复审核！");
        }
        //商品分类同步到供应商商品
        bankerProductMapper.updateCategoryId(product.getId(),product.getTopCategoryId(),product.getCategoryId(),product.getBottomCategoryId(),product.getAppraisalType());
        // 1.供应商产品-->普通产品库
        // 商品审核通过 状态为：待上架
        product.setBankerProductId(product.getId());
//        product.setAuditStatus(1);
        // 审核时不是0元测评商品时，isZeroProduct改为0-否
        if(product.getBankerColumnId() != 3) {
            product.setIsZeroProduct(0);
        } else {
            product.setIsZeroProduct(1);
//            product.setProductType(2);
//            if (com.alibaba.excel.util.StringUtils.isEmpty(product.getActivityType())) {
//                product.setActivityType(ActivityEnums.ZERO_EVALUATION.getActivityCode());
//            }
        }
        YxProduct insertProduct = new YxProduct();
        example.createCriteria().andEqualTo("bankerProductId", product.getId()).andIn("status", Arrays.asList(3, 5));
        List<YxProduct> existList = productMapper.selectByExample(example);

        BeanUtils.copyProperties(product, insertProduct);
        if (!CollectionUtils.isEmpty(existList)) {
            insertProduct.setId(existList.get(0).getId());
        }else {
            insertProduct.setId(null);

            // 8.供应商商品总数 +1
            bankerMapper.addProductNum(product.getBankerId());
        }
        // 审核通过 把商品名称赋值给长标题
        insertProduct.setLongTitle(product.getProductName());
        insertProduct.setBrandId(null);
        insertProduct.setStatus((byte) 4);
        Integer insertCount = 0;
        Integer updateCount = 0;
        if (null == insertProduct.getId()) {
            insertProduct.setCreateTime(new Date());
            //费率
            insertProduct.setRate(BigDecimal.valueOf(1));
            if (com.alibaba.excel.util.StringUtils.isEmpty(insertProduct.getRecDescr())){
                //推荐描述
                insertProduct.setRecDescr(insertProduct.getDescr());
            }
//            if (com.alibaba.excel.util.StringUtils.isEmpty(insertProduct.getRecommendDoc())){
//                //推荐文案
//                insertProduct.setRecommendDoc(insertProduct.getDescr());
//            }
            insertProduct.setRecDescr(null);
            insertProduct.setAppraisalType(null == product.getAppraisalType() ? 1 : product.getAppraisalType());
            insertCount = productMapper.insertSelective(insertProduct);
        }else {
            if (null == insertProduct.getBrandId()){
                insertProduct.setBrandId(0);
            }
            insertProduct.setRecDescr(null);
            insertProduct.setUpdateTime(new Date());
            updateCount = productMapper.updateByPrimaryKeySelective(insertProduct);
        }

        //主键自增拿到增加后的productId
        Integer productId = insertProduct.getId();

        // 2.同步商品img
//        if (updateCount > 0){
//            productImgMapper.deleteByProductId(productId);
//        }
        Example exampleImg = new Example(YxBankerProductImg.class);
        exampleImg.createCriteria().andEqualTo("productId", product.getId());
        List<YxBankerProductImg> bpImgList = bankerProductImgMapper.selectByExample(exampleImg);

        // 6.消息通知 供应商商品-->普通商品库
        YxBankerMessage message = new YxBankerMessage();
        message.setBankerId(product.getBankerId());
        message.setBankerProductId(product.getId());
        message.setProductName(product.getProductName());
        message.setRemarks("您的商品："+product.getProductName()+" 已成功通过审核！");
        message.setStatus(0);
        message.setType(1);
        message.setCreateTime(new Date());
        bankerMessageMapper.insert(message);

        // 7.商品审核通过，供应商商品状态修改为审核通过状态 官网栏目也做同步
        bankerProductMapper.updateAuditStatus(product.getId(), 1,product.getAppraisalType());
        bankerProductMapper.synvBankerColumn(product.getId(), product.getBankerColumnId());

        // 9.同步供应商 0元适用商品数量
        Integer zeroNum = productMapper.getZeroCount(product.getBankerId());
        bankerMapper.syncZeroNum(product.getBankerId(), zeroNum);

        //判断商品更改内容


        return insertCount > 0 ? insertCount: updateCount;
    }

    public Integer verifyFail(ProductVerifyRequest productVerifyRequest) {
        Integer count = bankerProductMapper.verifyFail(productVerifyRequest.getId(), productVerifyRequest.getVerifyRemarks());
        if (count > 0) {
            YxBankerMessage message = new YxBankerMessage();
            message.setBankerId(productVerifyRequest.getBankerId());
            message.setBankerProductId(productVerifyRequest.getId());
            message.setProductName(productVerifyRequest.getProductName());
            message.setRemarks("您的商品："+productVerifyRequest.getProductName()+" 未能成功通过审核！(原因："+productVerifyRequest.getVerifyRemarks()+")");
            message.setStatus(0);
            message.setType(2);
            message.setCreateTime(new Date());
            bankerMessageMapper.insert(message);
        }
        return count;
    }

}
